<template>
    <div>
<!--        <div class="crumbs">-->
<!--            <el-breadcrumb separator="/">-->
<!--                <el-breadcrumb-item>-->
<!--                    <i class="el-icon-lx-cascades"></i> 菜单管理-->
<!--                </el-breadcrumb-item>-->
<!--            </el-breadcrumb>-->
<!--        </div>-->
        <div class="container">
            <div class="handle-box">
                <el-input v-model="query.menuId" placeholder="菜单ID" class="handle-input mr10" @keyup.enter.native="handleSearch"></el-input>
                <el-input v-model="query.menuTitle" placeholder="菜单名称" class="handle-input mr10" @keyup.enter.native="handleSearch"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                <el-button type="el-button" icon="el-icon-delete" @click="handleReset">重置</el-button>
                <div class="right-items" style="float: right;">
                    <el-button type="primary" icon="el-icon-plus" @click="handleAdd">新增</el-button>
                </div>
            </div>
            <el-table
                    :data="tableData"
                    style="width: 100%"
                    row-key="menuId"
                    class="table"
                    border
                    lazy
                    :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
                    @selection-change="handleSelectionChange"
            >
                <el-table-column prop="menuId" label="菜单ID" width="200" align="center"></el-table-column>
                <el-table-column prop="menuTitle" label="菜单名称" align="center"></el-table-column>
                <el-table-column prop="menuIcon" label="菜单图标" align="center"></el-table-column>
                <el-table-column prop="menuIndex" label="菜单URL" align="center"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                                type="text"
                                icon="el-icon-edit"
                                @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                                type="text"
                                icon="el-icon-delete"
                                class="red"
                                @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                        background
                        layout="total, prev, pager, next"
                        :current-page="query.pageIndex"
                        :page-size="query.pageSize"
                        :total="pageTotal"
                        @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>
        <!-- 新增弹出框 -->
        <el-dialog title="新增" :visible.sync="addVisible" width="30%" :before-close="closeAdd">
            <el-form ref="addForm" :rules="addFormRules" :model="addForm" label-width="120px">
                <el-form-item label="菜单ID" prop="menuId">
                    <el-input v-model="addForm.menuId"></el-input>
                </el-form-item>
                <el-form-item label="菜单名称" prop="menuTitle">
                    <el-input v-model="addForm.menuTitle"></el-input>
                </el-form-item>
                <el-form-item label="菜单图标" prop="menuIcon">
                    <el-input v-model="addForm.menuIcon"></el-input>
                </el-form-item>
                <el-form-item label="菜单URL" prop="menuIndex">
                    <el-input v-model="addForm.menuIndex"></el-input>
                </el-form-item>
                <el-form-item label="菜单显示顺序" prop="menuOdr">
                    <el-input type="number" v-model="addForm.menuOdr"></el-input>
                </el-form-item>
                <el-form-item label="父菜单ID" prop="parentMenuId">
                    <el-input v-model="addForm.parentMenuId"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="closeAdd">取 消</el-button>
                <el-button type="primary" @click="saveAdd">确 定</el-button>
            </span>
        </el-dialog>
        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%" :before-close="colseEdit">
            <el-form ref="form" :rules="addFormRules" :model="form" label-width="120px">
                <el-form-item label="菜单ID" prop="menuId">
                    <el-input disabled v-model="form.menuId"></el-input>
                </el-form-item>
                <el-form-item label="菜单名称" prop="menuTitle">
                    <el-input v-model="form.menuTitle"></el-input>
                </el-form-item>
                <el-form-item label="菜单图标" prop="menuIcon">
                    <el-input v-model="form.menuIcon"></el-input>
                </el-form-item>
                <el-form-item label="菜单URL" prop="menuIndex">
                    <el-input v-model="form.menuIndex"></el-input>
                </el-form-item>
                <el-form-item label="菜单显示顺序" prop="menuOdr">
                    <el-input type="number" v-model="form.menuOdr"></el-input>
                </el-form-item>
                <el-form-item label="父菜单ID" prop="parentMenuId">
                    <el-input v-model="form.parentMenuId"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="colseEdit">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    import { menuData,doAdd,doUpdate,doDelete } from '../../../../api/system/menu/menu';
    export default {
        name: 'menus',
        data() {
            return {
                query: {
                    menuId: '',
                    menuTitle: '',
                    userAcct:localStorage.getItem('ms_userAcct'),
                    pageIndex: 1,
                    pageSize: 10
                },
                tableData: [],
                multipleSelection: [],
                editVisible: false,
                addVisible:false,
                pageTotal: 0,
                addForm:{},
                addFormRules: {
                    menuId: [
                        { required: true, message: '请输入菜单ID', trigger: 'blur' }
                    ],
                    menuTitle: [
                        { required: true, message: '请输入菜单名称', trigger: 'blur' }
                    ],
                    menuIcon: [
                        { required: true, message: '请输入菜单图标', trigger: 'blur' }
                    ],
                    menuIndex: [
                        { required: true, message: '请输入菜单URL', trigger: 'blur' }
                    ],
                    menuOdr: [
                        { required: true, message: '请输入菜单显示顺序', trigger: 'blur' }
                    ]
                },
                form: {},
                idx: -1,
                id: -1
            };
        },
        created() {
            this.getData();
        },
        methods: {
            // 获取后台数据
            getData() {
                menuData(this.query).then(res => {
                    if (res.success == true){
                        this.tableData = res.data;
                        this.pageTotal = res.total;
                    } else {
                        this.$message.error("查询失败！");
                        this.tableData = [];
                        this.pageTotal = 0;
                    }
                });
            },
            // 触发搜索按钮
            handleSearch() {
                this.$set(this.query, 'pageIndex', 1);
                this.getData();
            },
            // 触发重置按钮
            handleReset() {
                this.query.menuId = "";
                this.query.menuTitle = "";
                this.getData();
            },
            // 删除操作
            handleDelete(index, row) {
                // 二次确认删除
                this.$confirm('确定要删除吗？', '提示', {
                    type: 'warning'
                })
                    .then(() => {
                        //后台进行删除操作
                        doDelete({ menuId:row.menuId}).then(res => {
                            if (res.data==1){
                                this.$message.success('删除成功');
                                this.getData();
                            }else {
                                this.$message.error('删除失败');
                                this.getData();
                            }
                        });
                    })
                    .catch(() => {
                        this.$message({
                            message:'已取消删除'
                        });
                    });
            },
            // 多选操作
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            handleAdd(){
                this.addForm={};
                this.addVisible=true;
            },
            saveAdd(){
                this.$refs['addForm'].validate((valid) => {
                    if (valid) {
                        this.addForm.createUserAcct = localStorage.getItem('ms_userAcct');
                        //新增form表单提交到后台
                        doAdd(this.addForm).then(res => {
                            if (res.data == 1) {
                                this.addVisible = false;
                                this.getData();
                                this.$message.success('保存成功');
                            } else {
                                this.$message.error('保存失败');
                            }
                        });
                    }
                });
            },
            closeAdd(){//关闭新增弹出框
                this.$confirm('确认关闭？', '提示', {
                    type: 'warning'
                })
                    .then(() => {
                        this.addVisible=false;
                        this.addForm={};
                        this.getData();
                    })
                    .catch(() => {
                        this.$message({
                            message:'已取消操作'
                        });
                    });
            },
            // 编辑操作
            handleEdit(index, row) {
                this.idx = index;
                this.form = row;
                this.editVisible = true;
            },
            // 保存编辑
            saveEdit() {
                this.$refs['form'].validate((valid) => {
                    if (valid) {
                        this.editVisible = false;
                        this.form.children = [];
                        doUpdate(this.form).then(res => {
                            if (res.data == 1) {
                                this.editVisible = false;
                                this.form = {};
                                this.getData();
                                this.$message.success('编辑成功');
                            } else {
                                this.$message.error('编辑失败');
                            }
                        });
                    }
                });
            },
            colseEdit(){
                this.$confirm('确认关闭？', '提示', {
                    type: 'warning'
                })
                    .then(() => {
                        this.editVisible = false;
                    })
                    .catch(() => {
                        this.$message({
                            message:'已取消操作'
                        });
                    });

            },
            /*表单重置*/
            resetForm(refname){
                if (this.$refs[refname] !== undefined){
                    this.$refs[refname].resetFields();
                }
            },
            // 分页导航
            handlePageChange(val) {
                this.$set(this.query, 'pageIndex', val);
                this.getData();
            }
        }
    };
</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }
    .table {
        width: 100%;
        font-size: 14px;
    }
    .red {
        color: #ff0000;
    }
    .mr10 {
        margin-right: 10px;
    }
</style>
